function Addr(province, city, county){
	this.init(province, city, county);
}

(function(window){
	var _addr = {
		//加载区域地址
		areaData : {},
	};
	Addr.prototype = {
		init:function(province, city, county){
			this.provinceId = province;
			this.cityId = city;
			this.countyId = county;
		},
		initLoad:function(){
			var provinceCode="0",cityCode="0",countyCode="0";
			if($("#"+this.provinceId) && $("#"+this.provinceId).length){
				provinceCode=$("#"+this.provinceId).val();
			}
			if($("#"+this.cityId) && $("#"+this.cityId).length){
				cityCode=$("#"+this.cityId).val();
			}
			if($("#"+this.countyId) && $("#"+this.countyId).length){
				countyCode=$("#"+this.countyId).val();
			}
			this.loadArea(1, "0", provinceCode);
			if(provinceCode!="0"){
				this.loadArea(2, provinceCode, cityCode);	
			}
			if(cityCode!="0"){
				this.loadArea(3, cityCode, countyCode);	
			}
			var oThis = this;
			//省份点击事件
			$("#"+this.provinceId).change(function(){
				oThis.loadArea(2, $(this).val());	
			});
			//城市点击事件
			$("#"+this.cityId).change(function(){
				oThis.loadArea(3, $(this).val());	
			});
		},
		loadArea : function(level, superCode, code){
			if(superCode=="0" && level>1){
				$("#"+this.countyId).html('<option value="0">区/县</option>');
				if(level == 2) $("#"+this.cityId).html('<option value="0">城市</option>');
				return;	
			}
			if(level == 1 && (!code || code=="0")){
				$("#"+this.cityId).html('<option value="0">城市</option>');
				$("#"+this.countyId).html('<option value="0">区/县</option>');
			}else if(level == 2 && (!code || code=="0")){
				$("#"+this.countyId).html('<option value="0">区/县</option>');
			}
			if(JFT.isBlank(superCode)) return;
			if(_Addr.areaData[superCode]){
				this.renderArea(level, _Addr.areaData[superCode], code);
				return;	
			}
			var oThis = this;
			beginPageLoading();
			ajaxGet("baseAction.do?method=loadArea&superCode="+superCode,function(data){
				endPageLoading();
				if(data && data["resultCode"] != 0){
					alert(data["resultMsg"]);
					return ;
				}
				_Addr.areaData[superCode] = data["resultData"];
				oThis.renderArea(level , _Addr.areaData[superCode], code);
			});
		},
		//渲染地区
		renderArea : function(level, data, code){
			var optionStr = [], selectedStr, objEl, restOption, selected;
			if(level == 1){
				objEl=$("#"+this.provinceId);
				restOption='<option value="">省/直辖市</option>';
			}else if(level == 2){
				objEl=$("#"+this.cityId);
				restOption='<option value="0">城市</option>';
			}else if(level == 3){
				objEl=$("#"+this.countyId);
				restOption='<option value="0">区/县</option>';
			}
			optionStr.push(restOption);
			if(data && data.length){
				for(var i=0; i<data.length; i++){
					selected="";
					if(code && data[i]["CODE"]==code){
						selected=' selected="selected"';	
					}
					optionStr.push('<option value="'+data[i]["CODE"]+'" '+selected+'>'+data[i]["NAME"]+'</option>'); 
				}
				objEl.html(optionStr.join("")).show();
			}else{
				objEl.hide();
			}
			optionStr=restOption=null;
		}
	};
	window._Addr=_addr;
})(window);